<?php
/*
 * NetfilterEyes - 2008 - Anouar Belkacem
 * This code is a modified copy of the FirewallEyes project source code
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
// ****************************************************************************
// return the regexp index for $columnName
// ****************************************************************************
function authenticationCheck() {
	global $IPAuthentication,$allowedClientIP;
	if ($IPAuthentication) {
		if(!in_array($_SERVER["REMOTE_ADDR"],$allowedClientIP)) {
			exit();
		}
	}
}
// ****************************************************************************
// return the regexp index for $columnName
// ****************************************************************************
function getIndexForColumn($columnName,$logFields) {
	for($i=0; $i<count($logFields); $i++) {
		if($logFields[$i][0]==$columnName) {
			Return $logFields[$i][1];
		}
	}
}
// ****************************************************************************
// return true if all criteria matches
// ****************************************************************************
function criteriaMatches($criteria,$logFields,$infoTab,$exactSearch) {
	$returnValue=true;
	for($i=0; $i<count($logFields); $i++) {
		$currentColumn=$logFields[$i][0];
		$currentData=$infoTab[$logFields[$i][1]];
		if($currentCriteria=$criteria[$currentColumn]) { // if criteria exists
			// test
			if(!searchString ($currentData,$currentCriteria,$exactSearch)) {
				Return false;
			}
		}
	}
	Return $returnValue;
}
// ****************************************************************************
// return true strings founded
// ****************************************************************************
function searchString($haystack, $searchedWords,$exactSearch) {
	
	if($searchedWords[0]=="!") {
		$negate=true;
		$searchedWords=substr($searchedWords,1);
	}
	$returnValue=false;
	$wordTab=preg_split ("/[\s,]+/", $searchedWords);
	if($wordTab) {
		for($i=0; $i<count($wordTab); $i++) {
			if($currentWord=$wordTab[$i]) {
				// test
				if(($exactSearch ? $haystack==$currentWord : stristr ($haystack,$currentWord))) {
					$returnValue=true;
					break;
				}				
			}
			
		}		
	}
	if($negate) {
		Return (!$returnValue);
	} else {
		Return $returnValue;
	}
}

// ****************************************************************************
// change lines to resolved items
// ****************************************************************************
function resolvAll() {
	global $logFields,$infoTab,$resolvService,$indexForProtocol,$infoTabOriginal;
	for($i=0; $i<count($logFields); $i++) 
	{ 
		if($resolvService) {  
			if($logFields[$i][3]=="service") {
				$currentProtocolIndex=$indexForProtocol;
				$service=getservbyport($infoTab[$logFields[$i][1]],strtolower($infoTab[$currentProtocolIndex]));
				if($service) {
					$infoTabOriginal[$logFields[$i][1]]=$infoTab[$logFields[$i][1]];
					$infoTab[$logFields[$i][1]]=$service;
				}
			}
		}
	}
}


// ****************************************************************************
// fgetrs : read line and put pointer at the begining
// ****************************************************************************
function fgetrs($fileHandle) {
     while (ftell($fileHandle)>=0) {
			
           $char = fgetc($fileHandle); 
           if (ftell($fileHandle)==1)  {
			   fseek ($fileHandle,-1,SEEK_CUR);
			   return $char.$line;
           } 
		   
           if ($char == "\n" || ftell($fileHandle)==1)  {
			   fseek ($fileHandle,-2,SEEK_CUR);
			   return $line;
           } 
           else { 
			   fseek ($fileHandle,-2,SEEK_CUR);
               $line = $char . $line; 
           } 
       } 
       return $line; 
} 

?>